using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using Common;
using Common.Enum;
using Common.NotifyEvent;

namespace SSettings
{
    [Serializable]
    public class ColumnInfo : NotifyPropertyChanged
    {
        private bool _Display = true;

        private byte _Info = 4;

        private bool _Visibility = true;

        private bool _AllowDelete = true;

        private bool _AllowEditing = true;

        private string _Header = "Column";

        private EColumnType _Type;

        private string _FieldName;

        private int _FieldIndex = -1;

        private ObservableCollection<string> _DataSource = new ObservableCollection<string>();

        public bool Display
        {
            get
            {
                return _Display;
            }
            set
            {
                if (_Display != value)
                {
                    _Display = value;
                    OnPropertyChanged("Display");
                }
            }
        }

        public byte Info
        {
            get
            {
                return _Info;
            }
            set
            {
                if (_Info != value)
                {
                    _Info = value;
                    OnPropertyChanged("Info");
                }
            }
        }

        public bool Visibility
        {
            get
            {
                return _Visibility;
            }
            set
            {
                if (_Visibility != value)
                {
                    _Visibility = value;
                    OnPropertyChanged("Visibility");
                }
            }
        }

        public bool AllowDelete
        {
            get
            {
                return _AllowDelete;
            }
            set
            {
                if (_AllowDelete != value)
                {
                    _AllowDelete = value;
                    OnPropertyChanged("AllowDelete");
                }
            }
        }

        public bool AllowEditing
        {
            get
            {
                return _AllowEditing;
            }
            set
            {
                if (_AllowEditing != value)
                {
                    _AllowEditing = value;
                    OnPropertyChanged("AllowEditing");
                }
            }
        }

        public string Header
        {
            get
            {
                return _Header;
            }
            set
            {
                if (!(_Header == value))
                {
                    _Header = value;
                    if (string.IsNullOrEmpty(_Header))
                    {
                        _Header = "Column";
                    }
                    OnPropertyChanged("Header");
                }
            }
        }

        public EColumnType Type
        {
            get
            {
                return _Type;
            }
            set
            {
                if (_Type != value)
                {
                    _Type = value;
                    OnPropertyChanged("Type");
                }
            }
        }

        public string FieldName
        {
            get
            {
                return _FieldName;
            }
            set
            {
                if (!(_FieldName == value))
                {
                    _FieldName = value;
                    OnPropertyChanged("FieldName");
                }
            }
        }

        public int FieldIndex
        {
            get
            {
                return _FieldIndex;
            }
            set
            {
                if (_FieldIndex != value)
                {
                    _FieldIndex = value;
                    OnPropertyChanged("FieldIndex");
                }
            }
        }

        public ObservableCollection<string> DataSource
        {
            get
            {
                return _DataSource;
            }
            set
            {
                if (_DataSource != value)
                {
                    _DataSource = value;
                    OnPropertyChanged("DataSource");
                }
            }
        }

        internal ColumnInfo Clone()
        {
            ColumnInfo columnInfo = new ColumnInfo();
            columnInfo.Display = Display;
            columnInfo.Info = Info;
            columnInfo.Visibility = _Visibility;
            columnInfo.AllowDelete = _AllowDelete;
            columnInfo.AllowEditing = _AllowEditing;
            columnInfo.Header = _Header;
            columnInfo.Type = _Type;
            columnInfo.FieldIndex = _FieldIndex;
            columnInfo.FieldName = _FieldName;
            columnInfo.DataSource = new ObservableCollection<string>(DataSource);
            return columnInfo;
        }

        public bool IsCustomColumn()
        {
            if (string.IsNullOrEmpty(_FieldName))
            {
                return _FieldIndex >= 0;
            }
            return false;
        }

        public bool IsNewCustomColumn()
        {
            if (string.IsNullOrEmpty(_FieldName))
            {
                return _FieldIndex == -1;
            }
            return false;
        }

        public void SaveToStream(StreamEx stream)
        {
            stream.Write(PropertyKey.Key_ColumnInfo);
            List<PropertyKey> list = new List<PropertyKey>();
            List<byte[]> list2 = new List<byte[]>();
            list.Add(PropertyKey.Key_Visibility);
            list2.Add(BytesConverter.GetBytes(Visibility));
            if (string.IsNullOrEmpty(FieldName))
            {
                list.Add(PropertyKey.Key_AllowDelete);
                list2.Add(BytesConverter.GetBytes(AllowDelete));
                list.Add(PropertyKey.Key_AllowEditing);
                list2.Add(BytesConverter.GetBytes(AllowEditing));
                if (!string.IsNullOrEmpty(Header))
                {
                    list.Add(PropertyKey.Key_Header);
                    list2.Add(BytesConverter.GetBytes(Header));
                }
                list.Add(PropertyKey.Key_Type);
                list2.Add(BytesConverter.GetBytes((int)Type));
                list.Add(PropertyKey.Key_FieldIndex);
                list2.Add(BytesConverter.GetBytes(FieldIndex));
            }
            if (DataSource != null && DataSource.Count > 0)
            {
                list.Add(PropertyKey.Key_DataSource);
                StreamEx streamEx = new StreamEx();
                streamEx.Write(BytesConverter.GetBytes(DataSource.Count));
                foreach (string item in DataSource)
                {
                    streamEx.Write(item);
                }
                streamEx.Seek(0L, SeekOrigin.Begin);
                list2.Add(streamEx.ToArray());
                streamEx.Close();
            }
            stream.Write(PropertyKey.Key_Index);
            stream.WriteIndex(list, list2);
            stream.Flush();
        }

        public bool ReadFromStream(StreamEx stream)
        {
            PropertyKey propertyKey = stream.ReadKey();
            if (propertyKey != PropertyKey.Key_ColumnInfo)
            {
                stream.Seek(-4L, SeekOrigin.Current);
                return false;
            }
            propertyKey = stream.ReadKey();
            if (propertyKey == PropertyKey.Key_Index)
            {
                List<PropertyKey> keys;
                List<byte[]> dates;
                stream.ReadIndex(out keys, out dates);
                bool flag = true;
                for (int i = 0; i < keys.Count; i++)
                {
                    if (!flag)
                    {
                        return false;
                    }
                    switch (keys[i])
                    {
                        case PropertyKey.Key_Visibility:
                            Visibility = BytesConverter.BytesToBool(dates[i]);
                            break;
                        case PropertyKey.Key_AllowDelete:
                            AllowDelete = BytesConverter.BytesToBool(dates[i]);
                            break;
                        case PropertyKey.Key_AllowEditing:
                            AllowEditing = BytesConverter.BytesToBool(dates[i]);
                            break;
                        case PropertyKey.Key_Header:
                            Header = BytesConverter.BytesToString(dates[i]);
                            break;
                        case PropertyKey.Key_Type:
                            Type = (EColumnType)BytesConverter.BytesToInt(dates[i]);
                            break;
                        case PropertyKey.Key_FieldIndex:
                            FieldIndex = BytesConverter.BytesToInt(dates[i]);
                            break;
                        case PropertyKey.Key_DataSource:
                            {
                                DataSource = new ObservableCollection<string>();
                                StreamEx streamEx = new StreamEx();
                                streamEx.Write(dates[i]);
                                streamEx.Seek(0L, SeekOrigin.Begin);
                                int num = streamEx.ReadInt();
                                for (int j = 0; j < num; j++)
                                {
                                    string item = streamEx.ReadString();
                                    DataSource.Add(item);
                                }
                                streamEx.Close();
                                break;
                            }
                    }
                }
                return true;
            }
            stream.Seek(-4L, SeekOrigin.Current);
            while (true)
            {
                switch (stream.ReadKey())
                {
                    case PropertyKey.Key_ID:
                        stream.ReadInt();
                        break;
                    case PropertyKey.Key_Visibility:
                        Visibility = stream.ReadBool();
                        break;
                    case PropertyKey.Key_AllowDelete:
                        AllowDelete = stream.ReadBool();
                        break;
                    case PropertyKey.Key_AllowEditing:
                        AllowEditing = stream.ReadBool();
                        break;
                    case PropertyKey.Key_DisplayIndex:
                        stream.ReadInt();
                        break;
                    case PropertyKey.Key_Header:
                        Header = stream.ReadString();
                        break;
                    case PropertyKey.Key_Type:
                        Type = (EColumnType)stream.ReadInt();
                        break;
                    case PropertyKey.Key_StringFormat:
                        stream.ReadString();
                        break;
                    case PropertyKey.Key_FieldName:
                        FieldName = stream.ReadString();
                        break;
                    case PropertyKey.Key_FieldIndex:
                        FieldIndex = stream.ReadInt();
                        break;
                    case PropertyKey.Key_DataSource:
                        {
                            DataSource = new ObservableCollection<string>();
                            int num2 = stream.ReadInt();
                            for (int k = 0; k < num2; k++)
                            {
                                DataSource.Add(stream.ReadString());
                            }
                            break;
                        }
                    default:
                        stream.Seek(-4L, SeekOrigin.Current);
                        return true;
                }
            }
        }
    }
}
